package cn.edu.zucc.personplan.util;


import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

public class TimeUtil {
	
    /**
     * 把符合日期格式的字符串转换为日期类型
     */
    public static Date stringtoDate(String dateStr, String format) {
        Date d = null;
        SimpleDateFormat formater = new SimpleDateFormat(format);
        try {
            formater.setLenient(false);
            d = formater.parse(dateStr);
        } catch (Exception e) {
            // log.error(e);
            d = null;
        }
        return d;
    }
    
   
    public static java.sql.Timestamp strToSqlTime(String strTime,String formatStr) {
        Date date = stringtoDate(strTime, formatStr);
        return new java.sql.Timestamp(date.getTime());
    }
    
    public static java.sql.Timestamp dateToSqlTime(Date date) {
    	if(date==null) {
    		return null;
    	}
//        Date date = stringtoDate(strTime, formatStr);
        return new java.sql.Timestamp(date.getTime());
    }
    
    
    public static java.sql.Timestamp nowSqlTime() {
//    	Date date=new Date();
//    	Timestamp timestamp=Timestamp.
        return new java.sql.Timestamp(System.currentTimeMillis());
    }

    /*
     * 鐏忓棙妞傞梻瀛樺煈鏉烆剚宕叉稉鐑樻闂傦拷
     * https://www.cnblogs.com/qiantao/p/10218921.html
     */
    public static String stampToDate(String timeStampStr) {

        long timeStamp = new Long(timeStampStr);
        return stampToDate(timeStamp);

    }

    void stoD() {

    }

    static void test() throws InterruptedException, ParseException {

//        long l = sqlStampToMillionSeconds(nowSqlTime());
//        System.out.println("l");
//        System.out.println(l);
//
//        String timeStr = toTimeStr(new Date(), "yyyy-MM-dd");
//        System.out.println("timeStr");
//        System.out.println(timeStr);

        Timestamp sendDate = TimeUtil.nowSqlTime();
        System.out.println("sendDate");
        System.out.println(sendDate);

//        sqlStampToTimeStr()
//        toTimeStr()

//        String format = new SimpleDateFormat("yyyy-MM-dd").format(sendDate);
        String format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(sendDate);
        System.out.println("format");
        System.out.println(format);
        Date date = TimeUtil.sqlStampToDate(sendDate);
     
        String timeStr = TimeUtil.toTimeStr(date, "yyyy-MM-dd");
        System.out.println("timeStr");
        System.out.println(timeStr);
    }

    public static void main(String[] args) throws ParseException, InterruptedException {
        Date date = sqlStampToDate(nowSqlTime());
        Date date2 = sqlStampToDate(nowSqlTime());
        System.out.println("date");
        System.out.println(date);
        System.out.println(date2.getDay()-date.getDay());
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = sdf.format(date);
//        date getday 瀵啰鏁�

    }

    public static long sqlStampToMillionSeconds(Timestamp timestamp) throws ParseException {
        //閼惧嘲褰囪ぐ鎾冲閺冨爼妫�
//        Timestamp t = new Timestamp(new Date().getTime());
//        System.out.println("瑜版挸澧犻弮鍫曟？閿涳拷"+t);
        //鐎规矮绠熼弮鍫曟？閺嶇厧绱�
        Date date = sqlStampToDate(nowSqlTime());

//        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddhhmmss");
//        String str = dateFormat.format(timestamp);
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmm");
//        //濮濄倕顦╂潪顒佸床娑撶儤顕犵粔鎺撴殶
//        Date parse = sdf.parse(str);
//        System.out.println("parse");
//        System.out.println(parse);
        long millionSeconds = date.getTime();
//        long millionSeconds = sdf.parse(str).getTime();// 濮ｎ偆顫�
//        System.out.println("濮ｎ偆顫楅弫甯窗" + millionSeconds);
        return millionSeconds;
    }

    /**
     * 娑撳秷鍏橀悽锟� .. date 娴兼艾顦块崙鐘层亯
     * 閺�閫涚啊 閸欘垯浜掓担璺ㄦ暏娴滐拷
     * 閺夈儴鍤� https://blog.csdn.net/gkx_csdn/article/details/88421994
     *
     * @param timestamp
     * @return
     * @throws
     */
    public static Date sqlStampToDate(Timestamp timestamp)  {
        //閼惧嘲褰囪ぐ鎾冲閺冨爼妫�
//        Timestamp t = new Timestamp(new Date().getTime());
//        System.out.println("瑜版挸澧犻弮鍫曟？閿涳拷"+t);
        //鐎规矮绠熼弮鍫曟？閺嶇厧绱�
        return new Date(timestamp.getTime());
//        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddhhmmss");
//        String str = dateFormat.format(timestamp);
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmm");
//        //濮濄倕顦╂潪顒佸床娑撶儤顕犵粔鎺撴殶
//        Date parse = sdf.parse(str);
////        System.out.println("parse");
////        System.out.println(parse);
//
//        return parse;
//        long millionSeconds = sdf.parse(str).getTime();// 濮ｎ偆顫�
//        System.out.println("濮ｎ偆顫楅弫甯窗" + millionSeconds);
//        return millionSeconds;
    }

    public static String sqlStampToTimeStr(Timestamp timestamp, String timePattern) throws ParseException {
        String formatTime = new SimpleDateFormat(timePattern).format(timestamp);
        return formatTime;

    }


    public static String stampToDate(long timeStamp) {
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        long lt = new Long(s);
        Date date = new Date(timeStamp);
        res = simpleDateFormat.format(date);
        return res;
    }

    /*
     * 鐏忓棙妞傞梻纾嬫祮閹诡澀璐熼弮鍫曟？閹达拷
     */
    public static String dateToStamp(String s) throws ParseException {
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = simpleDateFormat.parse(s);
        long ts = date.getTime();
        res = String.valueOf(ts);
        return res;
    }

    public static long getUnixTimestamp() {

        return System.currentTimeMillis() / 1000;
    }

    public static long getUnixTimestamp(String timeStr, String timePattern) throws ParseException {
//        "2020-03-25 15:01:17"
//        "yyyy-MM-dd HH:mm:ss"
        Date date = new SimpleDateFormat(timePattern).parse(timeStr);
//        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(timePattern);
//        simpleDateFormat.format()
        //        System.out.println(timestamp);
        return date.getTime() / 1000;
    }

    public static String toTimeStr(Date date, String timePattern) {
//        "2020-03-25 15:01:17"
//        "yyyy-MM-dd HH:mm:ss"
        //        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(timePattern);
//        simpleDateFormat.format()
        //        System.out.println(timestamp);

        return new SimpleDateFormat(timePattern).format(date);
    }


}
